<template>
  <div class="app-container">
    <div class="filter-container" align="left">
      <!-- <el-input class="filter-item" placeholder="请输入类别" style="width: 200px;" v-model="search" />
      <el-button @click="searchByName_(search)" class="filter-item" type="primary" icon="el-icon-search"
        size="medium">搜索</el-button> -->
      <el-button class="filter-item" type="success" icon="el-icon-edit" @click="handleCreate" style="margin-left: 10px;"
        size="medium">新增宠物类别</el-button>
    </div>
    <el-table :data="petTypeList" border style="width: 70%;">
      <el-table-column label="序号" width="180" align="center">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="pettypeName" label="种类" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" plain type="primary" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" plain type="danger" @click="handleDelete(scope.row.pettypeId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹框 -->
    <el-dialog title="宠物类别信息" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="宠物类别" :label-width="formLabelWidth">
          <el-input v-model="form.pettypeName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogStatus === 'create' ? createData() : updateData()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { selectAll, add, update, del, searchByName } from '../../api/petmanage/pettype.js'

export default {
  data() {
    return {
      search: '',
      petTypeList: [],
      dialogFormVisible: false,
      dialogStatus: '',
      updateId: null,
      form: {
        pettypeId: '',
        pettypeName: '',
      },
      formLabelWidth: '120px'
    }
  },
  methods: {
    resetForm() {
      this.form = {
        pettypeId: undefined,
        pettypeName: '',
      }
    },
    handleCreate() {
      this.resetForm();
      this.dialogStatus = 'create';
      this.dialogFormVisible = true;
    },
    handleUpdate(row) {
      this.form = Object.assign({}, row)
      this.dialogFormVisible = true;
      this.dialogStatus = 'update';
      this.updateId = row.pettypeId;
    },
    // 新增
    async createData() {
      let res = await add(this.form);
      this.$notify({
        title: 'Success',
        message: 'Created Successfully',
        type: 'success',
        duration: 2000
      })
      this.dialogFormVisible = false;
      this.selectAll_()
    },
    // 更新
    async updateData() {
      this.form.pettypeId = this.updateId;
      let res = await update(this.form);
      this.$notify({
        title: 'Success',
        message: 'Update Successfully',
        type: 'success',
        duration: 2000
      })
      this.dialogFormVisible = false
      this.selectAll_()
    },
    // 删除
    async handleDelete(id) {
      console.log(id)
      let res = await del(id);
      console.log(res)
      this.$notify({
        title: 'Success',
        message: 'Delete Successfully',
        type: 'success',
        duration: 2000
      })
      this.selectAll_()
    },
    // 查询所有
    async selectAll_() {
      let res = await selectAll();
      console.log(res)
      this.petTypeList = res.data;
    },
    // 根据种类名查询
    // async searchByName_(params) {
    //   let res = await searchByName(params);
    //   this.petTypeList = res.data
    // },
  },
  created() {
    this.selectAll_()
  },
}
</script>